﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication1.Models;

namespace MvcApplication1.Controllers
{
    public class WorkOrderController : Controller
    {
        private ManHourManagerContext db = new ManHourManagerContext();

        //
        // GET: /WorkOrder/

        public ActionResult Index()
        {
            var workorders = db.WorkOrders.Include(w => w.Site);
            return View(workorders.ToList());
        }

        //
        // GET: /WorkOrder/Details/5

        public ActionResult Details(int id = 0)
        {
            WorkOrder workorder = db.WorkOrders.Find(id);
            if (workorder == null)
            {
                return HttpNotFound();
            }
            return View(workorder);
        }

        //
        // GET: /WorkOrder/Create

        public ActionResult Create()
        {
            ViewBag.SiteID = new SelectList(db.Sites, "SiteID", "SiteName");
            return View();
        }

        //
        // POST: /WorkOrder/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(WorkOrder workorder)
        {
            if (ModelState.IsValid)
            {
                db.WorkOrders.Add(workorder);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.SiteID = new SelectList(db.Sites, "SiteID", "SiteName", workorder.SiteID);
            return View(workorder);
        }

        //
        // GET: /WorkOrder/Edit/5

        public ActionResult Edit(int id = 0)
        {
            WorkOrder workorder = db.WorkOrders.Find(id);
            if (workorder == null)
            {
                return HttpNotFound();
            }
            ViewBag.SiteID = new SelectList(db.Sites, "SiteID", "SiteName", workorder.SiteID);
            return View(workorder);
        }

        //
        // POST: /WorkOrder/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(WorkOrder workorder)
        {
            if (ModelState.IsValid)
            {
                db.Entry(workorder).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.SiteID = new SelectList(db.Sites, "SiteID", "SiteName", workorder.SiteID);
            return View(workorder);
        }

        //
        // GET: /WorkOrder/Delete/5

        public ActionResult Delete(int id = 0)
        {
            WorkOrder workorder = db.WorkOrders.Find(id);
            if (workorder == null)
            {
                return HttpNotFound();
            }
            return View(workorder);
        }

        //
        // POST: /WorkOrder/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            WorkOrder workorder = db.WorkOrders.Find(id);
            db.WorkOrders.Remove(workorder);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}